package com.yz.book.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yz.book.entity.Cart;
import com.yz.book.entity.Product;
import com.yz.book.mapper.CartMapper;
import com.yz.book.mapper.ProductMapper;
import com.yz.book.service.CartService;
import com.yz.book.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ste
 * @since 2023-02-04 03:37:36
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    RedisTemplate<String, Object> stringObjectRedisTemplate;

    @Override
    public void addCart(Integer userId, Integer productId) {

        Product product = productMapper.selectById(productId);

        //加入购物车,数据存在redis中
        HashOperations<String, Object, Object> ops = stringObjectRedisTemplate.opsForHash();

        Object item = ops.get(userId + "", productId + "");
        System.out.println(item);

        if (item == null) {
            //该图书还没有加入过购物车   直接组装CartItem写入redis
            CartItem cartItem = new CartItem();
            cartItem.setUserId(userId);
            cartItem.setProductId(productId);
            cartItem.setProductName(product.getProductName());
            cartItem.setProductPrice(product.getProductPrice());
            cartItem.setProductImgsrc(product.getProductImgsrc());
            cartItem.setBuyCount(1);
            cartItem.setSumPrice(product.getProductPrice().multiply(new BigDecimal("1")));

            ops.put(userId + "", productId + "", cartItem);
        } else {
            //该图书已经加入过购物车  修改购买数量和小计即可.
            CartItem cartItem = (CartItem) item;
            cartItem.setBuyCount(cartItem.getBuyCount() + 1);  //购买数量+1
            //计算方法
            //方式一: price x buyCount
            //方式二: 原有sumPrice + price
            cartItem.setSumPrice(cartItem.getSumPrice().add(cartItem.getProductPrice()));

            ops.put(userId + "", productId + "", cartItem);
        }

    }

    @Override
    public Collection<Object> getItemByUserId(Integer userId) {
        HashOperations<String, Object, Object> ops = stringObjectRedisTemplate.opsForHash();
        Map<Object, Object> entries = ops.entries(userId + "");
        Collection<Object> allItems = entries.values();

        return allItems;
    }

    @Override
    public void updateItemNum(Integer userId, Integer productId, Integer buyCount) {
        HashOperations<String, Object, Object> ops = stringObjectRedisTemplate.opsForHash();
        CartItem cartItem = (CartItem) ops.get(userId + "", productId + "");

        if (buyCount == 1) {
            //购买数量+1

            cartItem.setBuyCount(cartItem.getBuyCount() + 1);  //购买数量+1
            cartItem.setSumPrice(cartItem.getSumPrice().add(cartItem.getProductPrice()));

            ops.put(userId + "", productId + "", cartItem);

        } else if (buyCount == -1) {
            //购买数量减一
            Integer newItemNum = cartItem.getBuyCount() - 1;
            if (newItemNum < 1) {
                //做删除操作
                ops.delete(userId + "", productId + "");
            } else {
                //更新操作
                cartItem.setBuyCount(cartItem.getBuyCount() - 1);  //购买数量+1
                cartItem.setSumPrice(cartItem.getSumPrice().subtract(cartItem.getProductPrice()));

                ops.put(userId + "", productId + "", cartItem);
            }

        } else {
            //认为是0,删除该项
            ops.delete(userId + "", productId + "");
        }

    }

    @Override
    public BigDecimal calTotalPrice(Integer userId, Integer[] productIds) {
        HashOperations<String, Object, Object> ops = stringObjectRedisTemplate.opsForHash();
        List<Object> allCartItems = ops.values(userId + "");

        //计算总价
        BigDecimal totalPrice = new BigDecimal("0.00");
        for (Object item : allCartItems) {
            CartItem cartItem = (CartItem) item;
            for (Integer productId : productIds) {
                //只有当前项的id在bookIds中时才作为计算总价的一部分
                if (cartItem.getProductId().equals(productId)) {
                    totalPrice = totalPrice.add(cartItem.getSumPrice());
                }
            }
        }

        return totalPrice;
    }
}
